// JavaScript Document
function copyDel(btn){
	var i;
	var dbg = "";
	
	var currentDel = getCurrentDel();
	var currentDelName = getCurrentDelName();
	if(currentDel){
		var dataOBJ = getDelObject(currentDel);
		
		if(dataOBJ){
			var cp = new Object();
			cp.delid = currentDel;
			cp.delname = currentDelName;
			cp.data = new Object();
			
			var adrese = Array();
			
			for(i=0; i<dataOBJ.childNodes.length; i++){
				var nn = dataOBJ.childNodes[i].nodeName;
				if(nn.match(/^fieldset/i)){
					adrese.push(dataOBJ.childNodes[i]);
				}
			}
			
			// data 1
			cp.data.profil_srp	= trim(document.getElementById("komdel_profil_srp_"+cp.delid).value);
			cp.data.profil_eng	= trim(document.getElementById("komdel_profil_eng_"+cp.delid).value);
			cp.data.email		= trim(document.getElementById("komdel_email_"+cp.delid).value);
			cp.data.web			= trim(document.getElementById("komdel_web_"+cp.delid).value);
			cp.data.brand		= trim(document.getElementById("komdel_brands_"+cp.delid).innerHTML);
			cp.data.kword_srp	= trim(document.getElementById("komdel_keywords_srb_"+cp.delid).value);
			cp.data.kword_eng	= trim(document.getElementById("komdel_keywords_eng_"+cp.delid).value);
						
			// data2
			cp.data.adrese = Array();
			for(i=0; i<adrese.length; i++){
				var adrOBJ = new Object();
				
				adrOBJ.www = adrese[i].getAttribute("www");
				var ext = adrese[i].id.replace("adresa_data_","");
				adrOBJ.ext = ext;
				
				adrOBJ.adresa_id	= document.getElementById("komdel_adresa_"+ext).value;
				adrOBJ.region_o		= document.getElementById("komdel_region_"+ext);	
				adrOBJ.region		= document.getElementById("komdel_region_"+ext).value;
				adrOBJ.opstina_o	= document.getElementById("komdel_opstina_"+ext);
				adrOBJ.opstina		= document.getElementById("komdel_opstina_"+ext).value;
				adrOBJ.mesto_o		= document.getElementById("komdel_mesto_"+ext);
				adrOBJ.mesto		= document.getElementById("komdel_mesto_"+ext).value;
				adrOBJ.lok_opstina_o= document.getElementById("komdel_lok_opstina_"+ext);
				adrOBJ.lok_opstina	= document.getElementById("komdel_lok_opstina_"+ext).value;
				adrOBJ.naselje_o	= document.getElementById("komdel_naselje_"+ext);
				adrOBJ.naselje		= document.getElementById("komdel_naselje_"+ext).value;
				adrOBJ.ulica_id		= document.getElementById("komdel_ulica_id_"+ext).value;
				adrOBJ.ulica		= document.getElementById("komdel_ulica_"+ext).value;
				adrOBJ.broj			= document.getElementById("komdel_broj_"+ext).value;
				adrOBJ.ptt			= document.getElementById("komdel_ptt_"+ext).innerHTML;
				
				adrOBJ.telefon		= document.getElementById("komdel_tel_"+ext).value;
				adrOBJ.faks			= document.getElementById("komdel_faks_"+ext).value;
				adrOBJ.rv_dan		= document.getElementById("komdel_rvreme_dan_"+ext).value;
				adrOBJ.rv_subota	= document.getElementById("komdel_rvreme_subota_"+ext).value;
				adrOBJ.rv_nedelja	= document.getElementById("komdel_rvreme_nedelja_"+ext).value;
				adrOBJ.cards_o		= document.getElementById("komdel_select_cards_"+ext);
				
				adrOBJ.cards = Array();
				var cards = document.getElementById("komdel_cards_"+ext).getElementsByTagName("li");
				if(cards.length>0){
					for(j=0; j<cards.length; j++){
						adrOBJ.cards.push(cards[j].cloneNode(true)); 
					}
				}
				
				cp.data.adrese.push(adrOBJ);
			}
			document.clipBoard = cp;
			document.getElementById("pasteMask").style.display="none";
			var pasteBtn = btn.previousSibling.previousSibling;
			pasteBtn.title = cp.delname;
			var clearCP = document.getElementById("delCPData");
			clearCP.style.display = "block";
		}
	}
}

function pasteDel(btn,komid){
	var cp = document.clipBoard
	if(cp){
		var currentDel = getCurrentDel();
		var dataOBJ = getDelObject(currentDel);
		while(dataOBJ.childNodes.length>0){
			dataOBJ.removeChild(dataOBJ.childNodes[0]);
		}
		dataOBJ.style.width = "100%";
				
		var _obj=null, _obj1=null;
	// DELATNOST DIV LEVO
		var div1 = document.createElement("div");
			dataOBJ.appendChild(div1);
			div1.style.position = "relative";
			div1.style.overflow = "hidden";
			div1.style.float = "left";
			div1.style.width = "470px";
			div1.style.height = "230px";
		var div2 = document.createElement("div");
			dataOBJ.appendChild(div2);
			div2.style.position = "relative";
			div2.style.margin = (-1)*parseInt(div1.style.height)+"px 0px 0px "+parseInt(div1.style.width)+"px";
			div2.style.float = "right";
			div2.style.width = "480px";
			div2.style.height = "230px";
			
			_obj = document.createElement("input");
			div1.appendChild(_obj);
			_obj.type="button";
			_obj.style.margin="50px -34px 0px 10px";
			_obj.className="import";
			_obj.title="Uvezi iz osnovnih podataka";
			_obj.value = "&#10136;";
			_obj.onclick = function(){importFromBasics(this);}
		
			_obj = document.createElement("label");
			div1.appendChild(_obj);
			_obj.setAttribute("for","komdel_profil_srp_"+currentDel);
			_obj.className = "srp";
			_obj.innerHTML = "Profil srpski:";
		
			_obj = document.createElement("textarea");
			div1.appendChild(_obj);
			_obj.name = "komdel_profil_srp_"+currentDel; _obj.id = _obj.name;
			_obj.className = "double";
			_obj.value = cp.data.profil_srp
			
			_obj = document.createElement("br");
			_obj.clear = "all";
			div1.appendChild(_obj);
		
			_obj = document.createElement("label");
			div1.appendChild(_obj);
			_obj.setAttribute("for","komdel_profil_eng_"+currentDel);
			_obj.className = "eng";
			_obj.innerHTML = "Profil engleski:";
		
			_obj = document.createElement("textarea");
			div1.appendChild(_obj);
			_obj.name = "komdel_profil_eng_"+currentDel; _obj.id = _obj.name;
			_obj.className = "double";
			_obj.value = cp.data.profil_eng
				
	// DELATNOST DIV DESNO
			_obj = document.createElement("label");
			div2.appendChild(_obj);
			_obj.style.width = "56px";
			_obj.setAttribute("for","komdel_email_"+currentDel);
			_obj.innerHTML = "E-mail:";
		
			_obj = document.createElement("textarea");
			div2.appendChild(_obj);
			_obj.name = "komdel_email_"+currentDel; _obj.id = _obj.name;
			_obj.className = "emailweb";
			_obj.setAttribute("rule",'^(.+@.+(\..+)+\n*)*$');
			_obj.setAttribute("type","multiitem");
			_obj.value = cp.data.email
		
			_obj = document.createElement("label");
			div2.appendChild(_obj);
			_obj.style.width = "60px";
			_obj.setAttribute("for","komdel_keywords_srb_"+currentDel);
			_obj.innerHTML = "Kljucne reci: [srpski]";
		
			_obj = document.createElement("textarea");
			div2.appendChild(_obj);
			_obj.name = "komdel_keywords_srb_"+currentDel; _obj.id = _obj.name;
			_obj.value = cp.data.kword_srp
		
			_obj = document.createElement("label");
			div2.appendChild(_obj);
			_obj.style.width = "56px";
			_obj.setAttribute("for","komdel_web_"+currentDel);
			_obj.innerHTML = "Web:";
		
			_obj = document.createElement("textarea");
			div2.appendChild(_obj);
			_obj.name = "komdel_web_"+currentDel; _obj.id = _obj.name;
			_obj.className = "emailweb";
			_obj.setAttribute("rule",'^(^(((ht|f)tp(s?):\/\/)|(www\.[^ \[\]\(\)\n\r\t]+)|(([012]?[0-9]{1,2}\.){3}[012]?[0-9]{1,2})\/)([^ \[\]\(\),;&quot;\'&lt;&gt;\n\r\t]+)([^\. \[\]\(\),;&quot;\'&lt;&gt;\n\r\t])|(([012]?[0-9]{1,2}\.){3}[012]?[0-9]{1,2})$)*$');
			_obj.setAttribute("type","multiitem");
			_obj.value = cp.data.web
		
			_obj = document.createElement("label");
			div2.appendChild(_obj);
			_obj.style.width = "60px";
			_obj.setAttribute("for","komdel_keywords_eng_"+currentDel);
			_obj.innerHTML = "Kljucne reci: [engleski]";
		
			_obj = document.createElement("textarea");
			div2.appendChild(_obj);
			_obj.name = "komdel_keywords_eng_"+currentDel; _obj.id = _obj.name;
			_obj.value = cp.data.kword_eng
		
			_obj = document.createElement("label");
			div2.appendChild(_obj);
			_obj.style.width = "56px";
			_obj.setAttribute("for","komdel_brands_"+currentDel);
			_obj.innerHTML = "Brendovi:";
			_obj.innerHTML += '<input title="dodaj brend" type="button" class="novi labelBtn" onclick="var s=prompt(\'Naziv brenda:\'); if(trim(s).length==0){return;} LoadRSDoc(\'brands.php?a=search&mod=komitent&ul=komdel_brands_'+currentDel+'&keyword=\'+s,\'ypAjaxForm\')"  />';
			_obj.innerHTML += '<input title="obriši označene brendove" type="button" class="delete labelBtn" onclick="if(confirm(\'Da li želite da obrišete označen/e brendove?\')){deleteKomitentiBrands(\'komdel_brands_'+currentDel+'\')}"  />';
		
			_obj = document.createElement("ul");
			div2.appendChild(_obj);
			_obj.className = "KomitentBrands";
			_obj.name = "komdel_brands_"+currentDel; _obj.id = _obj.name;
			_obj.innerHTML = cp.data.brand
			
			_obj1 = document.createElement("div");
			div2.appendChild(_obj1);
			_obj1.id="wwwInfoHolder_"+currentDel;
			_obj1.className = "wwwInfoHolder";
			
			_obj = document.createElement("input");
			_obj1.appendChild(_obj);
			_obj.type="button";
			_obj.value="";
			_obj.className = "wwwInfoSwitcher";
			_obj.style.margin = "65px 9px 0px 0px";
			_obj.onclick = function(){
				toggleWebInfo(this,komid,currentDel);
			}
		
	// ADRESE
		for(i=1; i<=cp.data.adrese.length; i++){
			var dd = cp.data.adrese[i-1];
			var ext = currentDel+"_"+i;
			
			var fs = document.createElement("fieldset");
			dataOBJ.appendChild(fs);
			//fs.style.width = "900px";
			fs.id = "adresa_data_"+currentDel+"_"+i;
			fs.setAttribute("www",dd.www);
			fs.style.padding="0px";
			fs.style.margin="5px 0px 5px 2px";
			fs.style.width = "939px";
			fs.style.height = "212px";
			
			var leg = document.createElement("legend");
			fs.appendChild(leg);
			leg.style.margin = "0px 0px 0px 7px";
			
			_obj = document.createElement("input");
			leg.appendChild(_obj);
			_obj.type="button";
			_obj.className = "add";
			_obj.value = "+";
			_obj.onclick = function(){
				addKomdelData(this,currentDel);
			}
			
			_obj = document.createElement("input");
			leg.appendChild(_obj);
			_obj.type="button";
			_obj.className = "add";
			_obj.style.marginLeft = "3px";
			_obj.value = "-";
			_obj.onclick = function(){
				removeKomdelData(this);
			}
			
			div1 = document.createElement("div");
			fs.appendChild(div1);
			div1.style.width = "447px";
			div1.style.height = "181px";
			div1.style.position="relative";
			div1.style.margin = "4px 0px 0px 7px";
			div1.id = "komdel_adresa_holder_"+ext;
			
			div2 = document.createElement("div");
			fs.appendChild(div2);
			div2.style.float="left";
			div2.style.width="457px";
			div2.style.height="150px";
			div2.style.margin="-180px 0px 0px 464px";
			div2.style.position="relative"
			div2.style.paddingLeft="10px";
			div2.style.borderLeft="1px dotted #9a9980";
			
		// ADRESA DATA 1
			_obj = document.createElement("input");
			div1.appendChild(_obj);
			_obj.type="hidden";
			_obj.name = "komdel_adresa_"+ext; _obj.id=_obj.name;
			_obj.value = dd.adresa_id;
			
			_obj = document.createElement("input");
			div1.appendChild(_obj);
			_obj.type="button";
			_obj.className = "import";
			_obj.onclick = function(){importFromBasics(this)}
			_obj.value = "&#10136;";
			_obj.title = "Uvezi iz osnovnih podataka"
			
			_obj = document.createElement("label");
			div1.appendChild(_obj);
			_obj.setAttribute("for","komdel_region_"+ext);
			_obj.style.width = "51px";
			_obj.innerHTML = "Region:";
			
			_obj = dd.region_o.cloneNode(true);
			div1.appendChild(_obj);
			_obj.name = "komdel_region_"+ext; _obj.id = _obj.name;
			
			_obj = document.createElement("label");
			div1.appendChild(_obj);
			_obj.setAttribute("for","komdel_opstina_"+ext);
			_obj.innerHTML = "Opština:";
			
			_obj = dd.opstina_o.cloneNode(true);
			div1.appendChild(_obj);
			_obj.name = "komdel_opstina_"+ext; _obj.id = _obj.name;
			
			_obj = document.createElement("label");
			div1.appendChild(_obj);
			_obj.setAttribute("for","komdel_mesto_"+ext);
			_obj.innerHTML = "Mesto:";
			
			_obj = dd.mesto_o.cloneNode(true);
			div1.appendChild(_obj);
			_obj.name = "komdel_mesto_"+ext; _obj.id = _obj.name;
			
			_obj = document.createElement("label");
			div1.appendChild(_obj);
			_obj.setAttribute("for","komdel_lok_opstina_"+ext);
			_obj.innerHTML = "Lok.Opština:";
			
			_obj = dd.lok_opstina_o.cloneNode(true);
			div1.appendChild(_obj);
			_obj.name = "komdel_lok_opstina_"+ext; _obj.id = _obj.name;
			
			_obj = document.createElement("label");
			div1.appendChild(_obj);
			_obj.setAttribute("for","komdel_naselje_"+ext);
			_obj.innerHTML = "Naselje:";
			
			_obj = dd.naselje_o.cloneNode(true);
			div1.appendChild(_obj);
			_obj.name = "komdel_naselje_"+ext; _obj.id = _obj.name;
			
			
			_obj = document.createElement("label");
			div1.appendChild(_obj);
			_obj.style.clear = "both";
			_obj.setAttribute("for","sel_komdel_ulica_"+ext);
			_obj.innerHTML = "Ulica:";
			
			_obj = document.createElement("input");
			div1.appendChild(_obj);
			_obj.type = "text";
			_obj.name = "komdel_ulica_"+ext; _obj.id = _obj.name;
			_obj.disabled=true;
			_obj.value = dd.ulica;
			
			_obj = document.createElement("input");
			div1.appendChild(_obj);
			_obj.type = "hidden";
			_obj.name = "komdel_ulica_id_"+ext; _obj.id = _obj.name;
			_obj.value = dd.ulica_id;
			
			_obj = document.createElement("input");
			div1.appendChild(_obj);
			_obj.className = "btnUlicaSelect";
			_obj.type = "button";
			_obj.name = "sel_komdel_ulica_"+ext; _obj.id=_obj.name;
			_obj.title = "promenite ulicu";
			_obj.onclick = function(){
				searchUlice(this,'komdel_ulica_'+ext,'komdel_ulica_id_'+ext);
			}
			
			_obj = document.createElement("label");
			div1.appendChild(_obj);
			_obj.className = "second";
			_obj.innerHTML = "Broj:";
			_obj.setAttribute("for","komdel_broj_"+ext);
			
			_obj = document.createElement("input");
			div1.appendChild(_obj);
			_obj.type = "text";
			_obj.name = "komdel_broj_"+ext; _obj.id=_obj.name;
			_obj.style.width = "30px";
			_obj.value = dd.broj;
			
			_obj = document.createElement("span");
			div1.appendChild(_obj);
			_obj.id = "komdel_ptt_"+ext;
			_obj.style.marginLeft="100px";
			_obj.style.width="40px";
			_obj.style.height="20px";
			_obj.style.overflow="hidden";
			_obj.className = "ptt";
			_obj.style.float = "right";
			_obj.innerHTML = dd.ptt;
			_obj.title = "poštanski broj";
			
			_obj = document.createElement("span");
			div1.appendChild(_obj);
			_obj.innerHTML = "PTT:";
			_obj.style.width = "50px";
			_obj.style.float = "left";
			_obj.style.display = "block";
			_obj.style.margin = "0px 0px 0px 382px";
			_obj.style.padding = "89px 0px 0px 0px";
			
			_obj = document.createElement("label");
			div1.appendChild(_obj);
			_obj.setAttribute("for","komdel_tel_"+ext);
			_obj.style.clear = "both";
			_obj.innerHTML = "Telefon:";
			
			_obj = document.createElement("textarea");
			div1.appendChild(_obj);
			_obj.name = "komdel_tel_"+ext; _obj.id = _obj.name;
			_obj.value = dd.telefon
			
			_obj = document.createElement("label");
			div1.appendChild(_obj);
			_obj.className = "second_row";
			_obj.setAttribute("for","komdel_faks_"+ext);
			_obj.innerHTML = "Faks:";
			
			_obj = document.createElement("textarea");
			div1.appendChild(_obj);
			_obj.name = "komdel_faks_"+ext; _obj.id = _obj.name;
			_obj.value = dd.faks
			
		// ADRESA DATA 1
			var div21 = document.createElement("div");
			div2.appendChild(div21);
			div21.title = "Radno vreme";
			div21.className = "fieldset";
			div21.id="rvreme";
			div21.style.width = "161px";
			div21.style.height = "90px";
			div21.style.margin = "-121px 0px 0px 30px";
			div21.style.clear = "none";
			div21.style.position = "relative";
			
			_obj = document.createElement("label");
			div21.appendChild(_obj);
			_obj.setAttribute("for","komdel_rvreme_dan_"+ext);
			_obj.innerHTML = "Radnim danom:";
			
			_obj = document.createElement("input");
			div21.appendChild(_obj);
			_obj.type="text";
			_obj.name = "komdel_rvreme_dan_"+ext; _obj.id = _obj.name;
			_obj.setAttribute("maxlength",15);
			_obj.value = dd.rv_dan;
			
			_obj = document.createElement("label");
			div21.appendChild(_obj);
			_obj.setAttribute("for","komdel_rvreme_subota_"+ext);
			_obj.innerHTML = "Subotom:";
			_obj.style.clear = "both";
			
			_obj = document.createElement("input");
			div21.appendChild(_obj);
			_obj.type="text";
			_obj.name = "komdel_rvreme_subota_"+ext; _obj.id = _obj.name;
			_obj.setAttribute("maxlength",15);
			_obj.value = dd.rv_subota;
			
			_obj = document.createElement("label");
			div21.appendChild(_obj);
			_obj.setAttribute("for","komdel_rvreme_nedelja_"+ext);
			_obj.innerHTML = "Nedeljom:";
			_obj.style.clear = "both";
			
			_obj = document.createElement("input");
			div21.appendChild(_obj);
			_obj.type="text";
			_obj.name = "komdel_rvreme_nedelja_"+ext; _obj.id = _obj.name;
			_obj.setAttribute("maxlength",15);
			_obj.value = dd.rv_nedelja;
		
		// ADRESA DATA 2
			var div22 = document.createElement("div");
			div2.appendChild(div22);
			div22.title = "Platne kartice";
			div22.className = "fieldset";
			div22.id="rvreme";
			div22.style.width = "212px";
			div22.style.height = "128px";
			div22.style.margin = "-121px 0px 0px 223px";
			div22.style.clear = "none";
			div22.style.position = "relative";
			
			_obj = dd.cards_o.cloneNode(true);
			div22.appendChild(_obj);
			_obj.name = "komdel_select_cards_"+ext; _obj.id = _obj.name;
			_obj.style.margin = "16px 5px 5px 5px";
			
			_obj = document.createElement("input");
			div22.appendChild(_obj);
			_obj.type = "button";
			_obj.value = "+";
			_obj.style.width = "20px";
			_obj.style.height = "20px";
			_obj.style.margin = "16px 5px 5px 0px";
			_obj.style.padding = "0px";
			_obj.onclick = function(){komitentAddCard(this)}
			
			_obj = document.createElement("input");
			div22.appendChild(_obj);
			_obj.type = "button";
			_obj.value = "-";
			_obj.style.width = "20px";
			_obj.style.height = "20px";
			_obj.style.margin = "16px 5px 5px 0px";
			_obj.style.padding = "0px";
			_obj.onclick = function(){komitentRemoveCard(this)}
			
			_obj = document.createElement("ul");
			div22.appendChild(_obj);
			_obj.className = "listBox";
			_obj.name = "komdel_cards_"+ext; _obj.id = _obj.name;
			for(x in dd.cards){
				
				var o = dd.cards[x];
				o.onclick = function(){
					if(!this.className.length){
						this.className='selected'
					}else{
						this.className=''
					}
				}
				o.ondblclick = function(){
					this.parentNode.removeChild(this);
				}
				o.onmousedown = function(){return false}
				o.title = "šifra: "+o.getAttribute("value");
				
				_obj.appendChild(o);
			}
		}
		
		_obj = document.createElement("br");
		_obj.clear = "all";
		dataOBJ.appendChild(_obj);
	}else{
		alert("Niste ništa iskopirali.");
	}
}
function clearCPData(btn){
	btn.style.display = "none";
	var pbtn = btn.previousSibling.previousSibling.previousSibling.previousSibling;
	var mask = pbtn.previousSibling.previousSibling;
	mask.style.display = "block";
	pbtn.title = "";
}


// -------------------------------------------------------------------------
function getCurrentDel(){
	var ret = false;
	var delSelect = document.getElementById("komitent_delatnosti");
	var lis = delSelect.getElementsByTagName("li");
	for(i=0; i<lis.length; i++){
		if(lis[i].className=="current"){
			ret = lis[i].id;
			break;
		}
	}
	return ret;
}
function getDelObject(id){
	var ret = false;
	var delHolder = document.getElementById("komitent_delatnosti_data");
	var dels = delHolder.getElementsByTagName("blockquote");
	for(i=0; i<dels.length; i++){
		if(dels[i].id==id){
			ret = dels[i];
			break;
		}
	}
	return ret;
}
function getCurrentDelName(){
	var ret = false;
	var delSelect = document.getElementById("komitent_delatnosti");
	var lis = delSelect.getElementsByTagName("li");
	for(i=0; i<lis.length; i++){
		if(lis[i].className=="current"){
			var tmp = lis[i].getElementsByTagName("span");
			if(tmp.length>0){
				ret = tmp[1];
				ret = ret.innerHTML.substr(7);
				break;
			}
		}
	}
	return ret;
}